﻿#region Copyright
//Copyright 2012 Phelon Aka. Jon H.
/*
    This file is part of BGBuddy - Ultimate PVP Suite.

    BGBuddy - Ultimate PVP Suite is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BGBuddy - Ultimate PVP Suite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Text.RegularExpressions;
using Styx.Plugins;
using Styx;
using Styx.Helpers;
using Styx.CommonBot.POI;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.Pathing;

namespace UltimatePvPSuite
{
    public static class FlagActions
    {
        private static string Faction = null;
        private static bool FlagExists = false;
        private static String Reg = "The (Horde|Alliance) Flag was (picked up|dropped) by (.+)!";
        private static Regex TheReg = new Regex(Reg, RegexOptions.IgnoreCase);
        private static WoWPoint DefaultFlagLocation = StyxWoW.Me.Location;
        public static bool Pulse()
        {
            if (FlagAssistance())
            {
                return true;
            }
            return false;
        }

        public static bool FlagAssistance()
        {
            if (UltimatePvPSuite.EnableReturnFlag || UltimatePvPSuite.EnableBannerCap)
            {
                //Initialize Variables
                WoWPoint moveTo = StyxWoW.Me.Location;
                string FactionBannerNormal = null;
                string FactionBannerLarge = null;
                string FactionFlag = null;
                string NeutralFlag = null;
                WoWGameObject obj = null;
                WoWUnit targetingme = null;

                switch (Battlegrounds.Current)
                {
                    case BattlegroundType.WSG:
                        DefaultFlagLocation = StyxWoW.Me.IsHorde
                                   ? new WoWPoint(915.5441f, 1433.672f, 345.553f)
                                   : new WoWPoint(1540.514f, 1481.022f, 352.606f);
                        break;

                    case BattlegroundType.TP:
                        DefaultFlagLocation = StyxWoW.Me.IsHorde
                                   ? new WoWPoint(1577.461f, 343.9889f, 2.416306f)
                                   : new WoWPoint(2116.939f, 190.3402f, 44.05256f);
                        break;
                }

                //Check Faction
                if (StyxWoW.Me.IsHorde)
                {
                    Faction = "Horde";
                    NeutralFlag = @"World\Generic\PVP\CTFflags\NeutralCTFflag.mdx";
                    FactionFlag = @"World\Generic\PVP\CTFflags\HordeCTFflag.mdx";
                    FactionBannerNormal = @"World\Generic\PVP\BattlefieldBanners\BattlefieldBannerAlliance.mdx";
                    FactionBannerLarge = @"World\Generic\PVP\BattlefieldBanners\BattlefieldBannerAllianceLarge.mdx";
                }
                if (StyxWoW.Me.IsAlliance)
                {
                    Faction = "Alliance";
                    NeutralFlag = @"World\Generic\PVP\CTFflags\NeutralCTFflag.mdx";
                    FactionFlag = @"World\Generic\PVP\CTFflags\AllianceCTFflag.mdx";
                    FactionBannerNormal = @"World\Generic\PVP\BattlefieldBanners\BattlefieldBannerHorde.mdx";
                    FactionBannerLarge = @"World\Generic\PVP\BattlefieldBanners\BattlefieldBannerHordeLarge.mdx";
                }

                //Looking for Game Objects
                obj = ObjectManager.GetObjectsOfType<WoWGameObject>().Where(
                                    u => (u.Model.Equals(FactionBannerNormal) || u.Model.Equals(FactionBannerLarge) || u.Model.Equals(NeutralFlag) ||
                                        u.Model.Equals(FactionFlag)) && u.DistanceSqr <= 35 * 35).OrderBy(u => u.Distance).
                                    FirstOrDefault();

                targetingme = ObjectManager.GetObjectsOfType<WoWUnit>().Where(
                                    u => u.IsTargetingMeOrPet).OrderBy(u => u.Distance).
                                    FirstOrDefault();

                //If true try to interact
                if (obj != null)
                {
                    if (StyxWoW.Me.Location.DistanceSqr(UltimatePvPSuite.InitialFlagLocation) < 10)
                    {
                        return false;
                    }
                    moveTo = WoWMathHelper.CalculatePointFrom(StyxWoW.Me.Location, obj.Location, obj.InteractRange - 2f);
                    if ((obj.ToString().Contains("Banner") && targetingme == null && UltimatePvPSuite.EnableBannerCap && targetingme == null) ||
                        (obj.ToString().Contains("Flag") && UltimatePvPSuite.EnableReturnFlag && obj.Location.Distance(DefaultFlagLocation) > 10))
                    {
                        if (Navigator.CanNavigateFully(StyxWoW.Me.Location, moveTo))
                        {
                            BotPoi.Current = new BotPoi(obj, PoiType.Harvest);
                            Navigator.MoveTo(moveTo);
                            if (obj.Distance < obj.InteractRange)
                            {
                                Logger.Write("Trying to get Flag: " + obj.Name.ToString());
                                obj.Interact();
                                UltimatePvPSuite.FlagExists = false;
                            }
                            return true;
                        }
                    }
                }
                else { UltimatePvPSuite.FlagExists = false; }
            }
            return false;
        }
    }
}
